package net.huashitong.user.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.huashitong.user.entity.Postil;
import net.huashitong.user.filter.PostilFilter;
import net.huashitong.user.service.PostilService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;

/**
 * 批注
 * @author lzr
 *
 */
@Controller
@RequestMapping("/app/postil")
public class PostilController extends JsonBaseController {

	@Autowired
	private PostilService postilService;
	@Override
	protected String getView(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * 保存批注
	 * @param filter
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/savePostil")
	public Message savePostil(@RequestBody PostilFilter filter){
		
		Postil postil = new Postil();
		postil.setUserId(filter.getUserId());
		postil.setPhotoId(filter.getPhotoId());
		postil.setDate(filter.getDate());
		postilService.save(postil);
		return coverMessage("200", "存储成功");
	}
	/**
	 * 查找所有批注
	 * @param filter
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryPostilAll")
	public Message queryPostilAll(@RequestBody PostilFilter filter){
		//查找该用户所有的批注日期
		List<String> dates = postilService.queryByUserId(filter.getUserId());
		List<Map<String, Object>> maps = new ArrayList<Map<String,Object>>();
		for (String date : dates) {
			Map<String, Object> map = new HashMap<String, Object>();
			List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
			List<Postil> postils = postilService.queryByDate(date,filter.getUserId());
			for (Postil postil : postils) {
				Map<String, Object> map2 = new HashMap<String, Object>();
				map2.put("sysId", postil.getSysId());
				map2.put("photoId", postil.getPhotoId());
				list.add(map2);
			}
			map.put("date", date);
			map.put("photoIds", list);
			maps.add(map);
		}
		return coverMessage("200", "数据获取成功", maps);
	}
	/**
	 * 删除批注
	 * @param filter
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deletePostil")
	public Message deletePostil(@RequestBody PostilFilter filter){
		List<String> ids = filter.getIds();
		for (String id : ids) {
			postilService.deleteById(id);
		}
		return coverMessage("200", "删除成功");
	}
}
